<template>
  <div class="art-pub-container">
    <el-form
      :model="pubForm"
      :rules="pubRules"
      ref="pubRef"
      label-width="100px"
    >
      <el-form-item label="文章标题" prop="title" placeholder="请填写文章标题">
        <el-input v-model="pubForm.title" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="文章分类" prop="cate_id">
        <el-select v-model="pubForm.cate_id" placeholder="请选择分类" clearable>
          <el-option
            v-for="item in cateList"
            :key="item.id"
            :label="item.cate_name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文章内容">
        <quill-editor v-model="pubForm.content"></quill-editor>
      </el-form-item>
      <el-form-item label="文章封面">
        <div class="cover-box">
          <img :src="pubForm.cover_img" alt="" class="cover-img" />
          <el-link @click="upload" type="primary" :underline="false"
            >+ 选择封面</el-link
          >
          <!--隐藏的输入框 -->
          <input
            type="file"
            style="display: none"
            accept="image/*"
            ref="iptFile"
            @change="iptBtn"
          />
        </div>
      </el-form-item>
      <el-form-item>
        <!-- 以上获取了4个参数,点击的时候再传入一个以完成/草稿 -->
        <el-button type="primary" @click="handbtn('已发布')"
          >发布文章</el-button
        >
        <el-button type="info" @click="handbtn('草稿')">存为草稿</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Btnarticle } from '../../src/api/art.js'
import { mapState } from 'vuex'
import defaultCover from '../assets/images/cover.jpg'
export default {
  name: '',
  data () {
    return {
      pubForm: {
        title: '', // 文章标题
        cate_id: '', // 文章分类id
        content: '', // 文章内容
        state: '', // 文章发布状态
        cover_img: defaultCover // 文章封面图
      },
      pubRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
          {
            min: 1,
            max: 30,
            message: '文章标题的长度为1-30个字符',
            trigger: 'blur'
          }
        ],
        cate_id: [
          { required: true, message: '请选择文章分类', trigger: 'blur' }
        ]
      },
      files: {}
    }
  },
  created () {},
  computed: {
    ...mapState('article', ['cateList'])
  },
  methods: {
    /* 点击 选择封面 */
    upload () {
      //  就把input type="file"  实现点击
      this.$refs.iptFile.click()
    },
    iptBtn (e) {
      //  选择了图片之后再触发
      /*
      1、获取选择的图片资源
      2、显示刚刚获取的图片到页面上
       */
      // console.log(123, '触发了隐藏域表单')
      // 1、获取选择的图片资源
      var file = e.target.files[0]
      if (file) {
        /* 将资源文件使用 URL.createObjectURL()  生成临时路径 */
        var tmp = URL.createObjectURL(file)
        console.log(tmp, 88)
        // 2-将图片临时路径存储起来给img
        this.pubForm.cover_img = tmp
        //  3-存储资源文件  =====用于后期上传的时候使用
        this.files = file
      }
    },
    async handbtn (state) {
      console.log(state)
      this.pubForm.state = state
      // 要求转化为formData格式

      const fm = new FormData()
      // console.log(fm)
      fm.append('title', this.pubForm.title)
      fm.append('cate_id', this.pubForm.cate_id)
      fm.append('content', this.pubForm.content)
      fm.append('state', this.pubForm.state)
      fm.append('cover_img', this.files)
      // this.pubForm.state = state
      const im = await Btnarticle(fm)
      console.log(im)
      if (im.data.code === 0) {
        // 如果发布成功,提示用户发布成功
        // alert('添加成功')
        this.$emit('changFs')
      }
    }
  }
}
</script>
<style lang="less" scoped>
.art-pub-container {
  padding: 0 30px 20px 0px;
}

.el-select {
  width: 100%;
}

/deep/ .ql-snow .ql-color-picker .ql-picker-label svg,
/deep/ .ql-snow .ql-icon-picker .ql-picker-label svg {
  position: relative;
  top: -6px;
}

/deep/ .ql-snow .ql-picker-label::before {
  position: relative;
  top: -8px;
}

/deep/ .ql-editor {
  min-height: 300px;
}

.cover-img {
  object-fit: cover;
  width: 400px;
  height: 280px;
  margin-right: 10px;
}

.cover-box {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
</style>
